<?php
// Generate random file name.
$ps = '';  
while(strlen($ps) < 9) {
     switch(rand(1,3)){  
     case 1:  
	  $ps = $ps.chr(rand(65,90));  
	  break;  
     case 2:  
	  $ps = $ps.chr(rand(97,122));  
	  break;  
     case 3:  
	  $ps = $ps.chr(rand(48,57));  
	  break;  
     }  
}

// Generate board_id file.
$f = fopen($ps . '.txt', 'w') or exit('unable to open file ');
fwrite($f, "@1020\r\n" . $_POST['comp_spec'] . "\r\nq\r\n");
fclose($f);
echo 'Board ID file is ' . $ps . '.txt <br /><br />';

// send a e-mail with board_id file.
$random_hash = md5(date('r', time()));
$to = $_POST['mail'];
$subject = '[ Board ID file ] ' . $_POST['subject'];
$headers = 'From: webmaster@tw.shuttle.com' . "\r\n" .  
	'Reply-To: webmaster@tw.shuttle.com' . "\r\n" .
	// The below is must exist for sending with attachment.
	'Content-Type: multipart/mixed; boundary="PHP-mixed-' . $random_hash . '"';
$attachment = chunk_split(base64_encode(file_get_contents($ps . '.txt'))); 
ob_start();
?>

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Board ID of <?php echo $_POST['subject']; ?> is <?php echo $_POST['comp_spec']; ?>.

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/text; name=<?php echo $ps; ?>.txt
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php 
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?> 
